
import time
import random
from neopixel import NeoPixel
from machine import Pin

#import setting
from 驱动.ws2812Screen import WS2812Screen
from setting import *

WIDTH = 32  # 屏幕宽
HEIGHT = 8  # 屏幕高

num_leds = SCREEN_WIDTH * SCREEN_HEIGHT  # 像素数量(led灯珠数量)

np = NeoPixel(Pin(LED_PIN, Pin.OUT), num_leds + 64)  # 创建像素序列对象

screen = WS2812Screen(np, SCREEN_WIDTH, SCREEN_HEIGHT)  # 创建屏幕对象




# 创建随机雨滴
def create_raindrop():
    x = random.randint(0, WIDTH - 1)
    speed = random.randint(1, 3)

    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    color = (r, g, b)
    return (x, 0, speed, color)

# 代码雨主逻辑
def code_rain():
    print("star1")
    screen.fill_screen()
    print("star2")
    screen.show()
    print("star3")

    raindrops = []

    while True:
        screen.fill_screen()
        # 创建新雨滴
        if len(raindrops) < 10:
            raindrops.append(create_raindrop())

        # 更新和绘制雨滴
        for i in range(len(raindrops)):
            x, y, speed, color = raindrops[i]

            # 绘制当前雨滴
            screen.draw_pixel(x, y, color)

            # 更新当前雨滴位置
            y += 1

            # 边界处理
            if y >= HEIGHT:
                raindrops[i] = create_raindrop()
            else:
                raindrops[i] = (x, y, speed, color)

            # 控制雨滴下落速度
            time.sleep(0.005)
        screen.show()

# 运行代码雨
code_rain()